package com.unitd.base.frame.spring.result;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;

/**
 * 自定义的controller向前端返回数据的数据类型类, 用于统一管理返回数据信息
 * @filename AjaxResult.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2017/4/1
 */
public class AjaxResult implements Serializable {

	/**
	 * 接口返回码, 200表示成功, 其他看对应的定义
	 * 推荐的做法是: 200: 成功; 非20:表示已知的异常(例如提示错误等, 需要调用地方单独处理);<0:表示未知的异常(不需要单独处理, 调用方统一处理)
	 */
	private int code;
	/** 返回的信息(主要出错的时候使用) */
	@JsonInclude(JsonInclude.Include.NON_NULL)
	private String msg;
	/** 返回的数据 */
	@JsonInclude(JsonInclude.Include.NON_NULL)
	private Object data;

	/* ************** 定义返回代码常量 ***************/
	/** 处理成功 */
	private static final int SUCCESS = 200;
	/** 处理失败 */
	public static final int FAIL = -200;
	/** 未登录 */
	public static final int NO_LOGIN = 401;
	/** 未知异常 */
	public static final int UNKNOWN_EXCEPTION = 99;

	public AjaxResult() {
		super();
	}

	public AjaxResult(Object data) {
		this(SUCCESS, null, data);
	}

	public AjaxResult(Throwable e) {
		this(FAIL, e.getMessage(), null);
	}

	public AjaxResult(String msg) {
		super();
		this.code = FAIL;
		this.msg = msg;
	}

	public AjaxResult(String msg, Object data) {
		super();
		this.code = FAIL;
		this.msg = msg;
		this.data = data;
	}

	public AjaxResult(int code, String msg, Object data) {
		super();
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}
}